package xyz.xuanlee.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import xyz.xuanlee.springcloud.entities.CommonResult;
import xyz.xuanlee.springcloud.entities.Payment;
import xyz.xuanlee.springcloud.service.PaymentService;

import javax.annotation.Resource;
import java.util.UUID;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private String SERVICE_PORT;
    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/payment/create")
    public CommonResult<Integer> create(@RequestBody Payment payment) {
        Integer result = paymentService.create(payment);
        log.info("insert: " + result);

        if (result > 0) {
            return new CommonResult<>(200, "success, service port:" + SERVICE_PORT, result);
        } else {
            return new CommonResult<>(444, "fail, service port:" + SERVICE_PORT);
        }
    }

    @GetMapping("/payment/getPayment/{id}")
    public CommonResult<Payment> create(@PathVariable("id") Long id) {
        Payment result = paymentService.getPaymentById(id);
        log.info("query: " + result);

        if (result != null) {
            return new CommonResult<>(200, "success, service port:" + SERVICE_PORT, result);
        } else {
            return new CommonResult<>(444, "fail, service port:" + SERVICE_PORT);
        }
    }

    @GetMapping("/payment/consul")
    public String paymentzk() {
        return "springcloud with consul: " + SERVICE_PORT + " serial no: " + UUID.randomUUID();
    }
}
